package com.cq.smartelebicycle.fragment;


import com.cq.smartelebicycle.R;

import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;


public class GPSFragment extends Fragment implements LocationListener,BaiduMap.OnMarkerClickListener,BaiduMap.OnMapStatusChangeListener{
	
	public static final String TAG_ID = "GPSFragment";
	
	private static final String TAG="GPSFragment";
    private TextView mTv;
    private MapView mMapView = null;
    private LocationManager mLocationManager;
    private BaiduMap mMap;
    private UiSettings mUiSettings;
    private Location mLocMyLocation;
    private InfoWindow mInfoWindow;
    private double mLatitude,mLongitude;
    Criteria mCriteria=new Criteria();
    private String mStrLocationProvider;
    private boolean isFirstLoc = true;
    private OverlayOptions myLocationOverlay = null;
    private String keyString = "BB9715B075470BD1473AD7D75F93893918C5FD47";

    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.gps_fragment, container,false);
        mMapView = (MapView)view.findViewById(R.id.bmapsView);
        mMap=mMapView.getMap();
        //去掉baidu logo
        mMapView.removeViewAt(1);

        mMap.setMyLocationConfigeration(new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING,true,null));
        mMap.setMyLocationEnabled(true);
        mUiSettings=mMap.getUiSettings();
        //指北针位置
        mUiSettings.setCompassEnabled(false);
        defineCriteria();
        mTv=(TextView)view.findViewById(R.id.info);
        mLocationManager=(LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE);
        mStrLocationProvider=mLocationManager.getBestProvider(mCriteria,true);
        mTv.setText(mStrLocationProvider);
        mLocMyLocation=mLocationManager.getLastKnownLocation(mStrLocationProvider);
        updateLocation(mLocMyLocation);
        //扫描时间
        mLocationManager.requestLocationUpdates(mStrLocationProvider,2000,10,this);


        return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}


    public GPSFragment() {
        super();
    }

    private void defineCriteria(){
        mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        mCriteria.setPowerRequirement(Criteria.POWER_LOW);
        mCriteria.setCostAllowed(true);
        mCriteria.setAltitudeRequired(false);
        mCriteria.setBearingRequired(false);
    }

    private void updateLocation(Location location){
        if(location !=null){
            mLocMyLocation=location;
            mTv.setText(location.getLatitude()+"\n" +location.getLongitude() );
            MyLocationData locationData=new MyLocationData.Builder().direction(0).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
            mMap.setMyLocationData(locationData);

            if(isFirstLoc){
                isFirstLoc=false;
                LatLng ll=new LatLng(location.getLatitude(),location.getLongitude());
                MapStatusUpdate u= MapStatusUpdateFactory.newLatLng(ll);
                mMap.animateMapStatus(u);
            }
        }else{
            mTv.setText("location null" );
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mLocationManager.removeUpdates(this);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {
        if (!mMap.getUiSettings().isCompassEnabled()) {
            mMap.getUiSettings().setCompassPosition(new Point(50,50));
            mMap.getUiSettings().setCompassEnabled(true);
        }

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void onLocationChanged(Location location) {
        mTv.setText(location.getLatitude()+"\n"+location.getLongitude());
        updateLocation(location);

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        mTv.setText("123");
        if(status!= LocationProvider.OUT_OF_SERVICE){
            updateLocation(mLocationManager.getLastKnownLocation(mStrLocationProvider));
        }else{
            mTv.setText("out of service");
        }

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
